package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class DeadLetterMqConfig {
    // 声明一些变量

    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    /**
     * 创建交换机
     */
    @Bean
    public DirectExchange exchange() {

        return new DirectExchange(exchange_dead, true, false, null);
    }


    /**
     * 创建队列二
     */
    @Bean
    public Queue queue2() {


        return new Queue(queue_dead_2, true, false, false, null);
    }

    /**
     * 创建队列一
     */
    @Bean
    public Queue queue1() {

        Map<String, Object> arguments = new HashMap<>();
        //指定死信交换机
        arguments.put("x-dead-letter-exchange", exchange_dead);
        //指定死信路由
        arguments.put("x-dead-letter-routing-key", routing_dead_2);

        //设置有效时间
        arguments.put("x-message-ttl", 10 * 1000);


        return new Queue(queue_dead_1, true, false, false, arguments);
    }


    /**
     * 绑定队列一
     */
    @Bean
    public Binding binding1() {


        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
    }

    /**
     * 绑定队列二
     */
    @Bean
    public Binding binding2() {


        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }
}


